package com.example.database.service.impl;

import com.example.database.dao.ClassInstructorDao;
import com.example.database.dao.StudentDao;
import com.example.database.entity.AdmissionForm;
import com.example.database.entity.ClassInstructor;
import com.example.database.entity.LeavingForm;
import com.example.database.service.ClassInstructorService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpSession;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.List;

@Service
public class ClassInstructorServiceImpl implements ClassInstructorService
{
    @Resource
    private ClassInstructorDao classInstructorDao;

    @Resource
    private StudentDao studentDao;

    @Override
    public boolean login(Integer teacherId, String password, HttpSession session)
    {
        ClassInstructor classInstructor = classInstructorDao.findClassInstructorById(teacherId);
        if (classInstructor != null && password.equals(classInstructor.getPassword()))
        {
            session.setAttribute("classInstructor", classInstructor);
            return true;
        }
        return false;
    }

    @Override
    public List<AdmissionForm> findAdmissionFormByClass(Integer classId)
    {
        return studentDao.findAdmissionFormByClass(classId);
    }

    @Override
    public List<LeavingForm> findLeavingFormByClass(Integer classId)
    {
        return studentDao.findLeavingFormByClass(classId);
    }

    @Override
    public Boolean processAdmissionFormByClassInstructor(Integer formId, boolean operation, String reason)
    {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return studentDao.processAdmissionFormByClassInstructor(formId, operation, df.format(System.currentTimeMillis()), reason);
    }

    @Override
    public Boolean processLeavingFormByClassInstructor(Integer formId, boolean operation, String reason)
    {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return studentDao.processLeavingFormByClassInstructor(formId, operation, df.format(System.currentTimeMillis()), reason);
    }
}
